<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pages_cat extends CI_Controller 
{
	protected $num_lang ;
	function __construct()
	{
		parent::__construct();
		
		//load helper
		//	$this->load->helper('login_helper');
		
			//load model
	$this->load->model('Mpages_cat');
	$this->load->model('Msetting');
	$this->num_lang = $this->Msetting->get_lang();
    //load libraries
    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
			
	// check login
	check_login();
	}
	
	function index($page='')
	{
	
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/pages_cat/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mpages_cat->count_all();

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Mpages_cat->list_data($page,$limit);// $this->Muser->user_review($page, $limit);

    $data['title'] ='Quản lý loại trang ';//$this->lang->line('title_pages_review');

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'page_cat_review';

    $this->load->view('admin/layout/template_main', $data);
	}
	
	
	
	//***********update  *************//
	
	function update()
	{
		$id= $this->uri->segment(4);
	 if ($this->input->post('update'))

    {
	
		
		  //set rules
	//	if($this->num_lang >1)
  			 $this->form_validation->set_rules('title_en', 'Tên tiêu đề tiếng Anh', 'required');

      $this->form_validation->set_rules('title_vi', 'Tên tiêu đề', 'required');


    

		  if ($this->form_validation->run() && $this->sub_update())

        redirect('admin/pages_cat/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/pages_cat/', 'refresh');

		}
		
			 //reload template

		$data['num_lang'] = $this->num_lang;
		$data['result']= $this->Mpages_cat->info_form_id($id);
		$data['title'] =  $this->lang->line('title_pages_update');
		$data['main'] = 'page_cat_update';
		$this->load->view('admin/layout/template_main', $data);
	}
	function sub_update()
	{
		$id= $this->uri->segment(4);
		if(isset($_POST["update"]))
		{
			$status =$this->input->post("status");
			$cate =  $this->input->post("cate");
			$modify = time();
			$data = array(
			"cat_name" => htmlentities($this->input->post("title_vi"), ENT_QUOTES, 'UTF-8'),
			"cat_status" =>  $status,
			'cat_modify'=>$modify,			
			);
			$data2 = array(
			"cat_name" => htmlentities($this->input->post("title_en"), ENT_QUOTES, 'UTF-8'),
			"cat_status" =>  $status,
			'cat_modify'=>$modify,			
			);
		if(($this->num_lang > 1 || 1==1)?$this->Mpages_cat->update($data,$id,1)&&$this->Mpages_cat->update($data2,$id,2):$this->Mpages_cat->update($data,$id,1))
			return true;
		else
			return false;
		}
		else
			return false;
		
	
	
	}
	
	function create()
	{
	 if ($this->input->post('create'))

    {
	

      //set rules
//	if($this->num_lang >1)
    $this->form_validation->set_rules('title_en', 'English Name', 'required');

      $this->form_validation->set_rules('title_vi', 'Vietnamese Name', 'required');

    




      if ($this->form_validation->run() && $this->sub_create())

        redirect('admin/pages_cat', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/pages_cat', 'refresh');

		}



	$data['num_lang'] = $this->num_lang;
		
		$data['main'] = 'page_cat_create';

    $data['title'] =  $this->lang->line('title_pages_create');

		$this->load->view('admin/layout/template_main', $data);
	}
	function sub_create()
	{
		
		if(isset($_POST["create"]))
		{
				$cate =  $this->input->post("cate");
			$pageid = $this->Mpages_cat->get_last_id();
			//print_r($pageid);
			if(!isset($pageid['cat_id']))
				$pid = 1;
			else
				$pid =$pageid['cat_id']+1;
			$status = $this->input->post('status');
			$modify = time();
			$data = array(
			"cat_id" => $pid,
			'cat_lang'=>'1',
			"cat_name" => htmlentities($this->input->post("title_vi"), ENT_QUOTES, 'UTF-8'),

			'cat_modify'=>$modify,			
			'cat_status'=>$status,
			);
			$data2 = array(
			"cat_id" => $pid,
			'cat_lang'=>'2',
			"cat_name" => htmlentities($this->input->post("title_en"), ENT_QUOTES, 'UTF-8'),
			'cat_modify'=>$modify,			
			'cat_status'=>$status,
			
			);
			
			
		if(($this->num_lang >1 || 1==1 )? $this->Mpages_cat->insert($data) && $this->Mpages_cat->insert($data2):$this->Mpages_cat->insert($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
	
	
	}

	
	
	
	function request()

	{

		//determine 'action'
	 

		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{

			if (in_array($key, $require))

			{

				$act = $key;
				
			}

		}



		//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Mpages_cat->delete();

				} break;

			}//end switch



			redirect('admin/pages/', 'refresh');

		}//end if

	}
	public function delete($id)
	{
		if($id >0 )
		
			$this->Mpages_cat->del_item($id);
		redirect('admin/pages_cat/', 'refresh');
		
	}
}









?>
